﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using NotepadDemo.Helps;
using NotepadDemo.Models;

namespace NotepadDemo.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            bool pass = false;
            string status = Request["status"];
            if (!string.IsNullOrEmpty(status))
            {
                pass = true;
            }

            string serverPath = Server.MapPath("/Content");

            if (!Directory.Exists(serverPath))
                Directory.CreateDirectory(serverPath);

            string fileName = Request["fileName"];
            if (!string.IsNullOrEmpty(fileName))
            {

                var data = MyExtensionMethods.GetData(serverPath + '/' + fileName);
                if (pass)
                {
                    ViewData["IsLocked"] = data.IsLocked.ToString();
                    ViewData["Password"] = data.Password;
                    ViewData["Uri"] = data.Uri;
                    ViewBag.content = data.Content;
                    return View();
                }


                if (string.IsNullOrEmpty(data.Password))
                {
                    ViewData["IsLocked"] = data.IsLocked.ToString();
                    ViewData["Password"] = data.Password;
                    ViewData["Uri"] = data.Uri;
                    ViewBag.content = data.Content;
                }
                else
                    return Redirect("/Notepad/Index?fileName=" + fileName);

            }
            else
            {
                string newName = Guid.NewGuid().ToString().Substring(0, 5);
                return Redirect("/Home/Index?fileName=" + newName);

            }

            return View();

        }
    }

}